package com.qyer.util.log.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.io.Text;

public class TrackSourceType {
	
	static  final String FS ="@"; 
	
	static String[] arrDomain = {".baidu.", ".google.", "so.360.",".so.com",".sogou.com", ".bing.com", 
		".ask.com", ".yahoo.", ".soso.", ".sm.", "haosou.com"};

	public static String getSourceType(String refer , String url , Map<String , String>sourceMap ,  Map<String , String>cateMap , String ua) {
		
		String res= getSource4OtherSite(refer ,  url , sourceMap , ua);
		
		if(res==null){
			res=getSource4InSite(refer  , cateMap);
		}
		return res; 
	}
	
	public static String getSource4OtherSite(String refer , String url , Map<String , String>sourceMap , String ua) {
		String res = null;
		int passd=0 ; 
		
		if(isMatch(refer, "weibo.")||isMatch(url, "weibo")){
			 res=  sourceMap.get("weibo"); 
			 if(isMatch(url, "weibo_go2eu")){
				 res=  sourceMap.get("weibo_go2eu"); 
			 }else if(isMatch(url, "weibo_lastminute")){
				 res=  sourceMap.get("weibo_lastminute"); 
			 }
		 }else if(isMatch(refer, ".weixin.")||isMatch(url, "weixin")){
			 res=  sourceMap.get("weixin"); 
			 if(isMatch(url, "weixinlm")){
				 res=  sourceMap.get("weixinlm"); 
			 }else if(isMatch(url, "campaign=weixin")&&!isMatch(url, "weixinlm")){
				 res=  sourceMap.get("campaign=weixin"); 
			 }
		 }else if(isMatch(url, "utm_source")||isMatch(refer, "utm_source")){
			 res=  sourceMap.get("sem"); 
		 }else if(isMatch(url, "	=app_")&&!isMatch(url, "campaign=app_share")){
			 res=  sourceMap.get("campaign=app_"); 
		 }else if(isMatch(url, "campaign=")||isMatch(url, "vote.php")){//sem||campaign
			 
			 if(isMatch(url, "campaign=baidu")||isMatch(url, "campaign=360sem")||isMatch(url, "wbad")){
				 res=  sourceMap.get("sem"); 
			 }else{
				 //campaign
			 }
			 
			 if(isMatch(url, "campaign=edm")){
				 res=  sourceMap.get("edm"); 
			 }else{
				 //campaign
			 }
			 
		 }else if(null!=refer&&!refer.equals("-")&&!refer.equals("None")&&!refer.equals("")&&!refer.equals("null")){
			 for(String seo:arrDomain){
				 if(isMatch(refer, seo)){
					 res=  sourceMap.get("seo"); 
					 passd=1 ; 
					 break;
				 }
			 }
			 if((passd==0&&!isMatch(refer, ".qyer."))){
				 res=  sourceMap.get("site_other"); 
			 }
			 
		 }else if(null==refer||refer.equals("-")||refer.equals("None")||refer.equals("")||refer.equals("null")){
			 if(ua.toLowerCase().contains("baiduboxapp")||ua.toLowerCase().contains("sogousearch")){
				 res=  sourceMap.get("seo"); 
			 }else{
				 res=  sourceMap.get("direct"); 
			 }
		 }
		 return res; 
	}
	

	public static String getSource4InSite(String refer  , Map<String , String>cateMap) {
		
		String res = CateUtil.getCate(refer, cateMap); 
		
		res ="2"+FS+res;
		
		return res; 
	}
	
	public static boolean isMatch(String url,String param){
		if(null!=param&&null!=url){
			if(url.indexOf(param)!=-1){
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
			
		String url ="plan.qyer.com/sld/sllsdfj";
		String refer ="https://www.baidu.com/";
		
		
		Map<String ,String > cateMap =getCateMap (); 
		
		Map<String ,String > sourceMap =getSourceMap(); 
		
		String cate = getSourceType( refer , url , sourceMap , cateMap , "") ; 
		
		System.out.println(cate);
	}
	
	public static Map<String ,String> getCateMap (){
		Map<String ,String > cateMap = new HashMap<String ,String  >();
		
		BufferedReader in = null;

		try {
			// 从当前作业中获取要缓存的文件
//			Path[] paths = DistributedCache.getLocalCacheFiles(job);
			
//			System.out.println("paths===="+paths);
			String line = null;
			String[] strArr = null;
			
			File path = new File ("/Users/sunhongxi/Documents/sshfileupdown/cate_business.txt") ;
			
			in = new BufferedReader((new InputStreamReader(new FileInputStream(path),"utf-8")));
			while (null != (line = in.readLine())) {
				strArr = line.split("\t", -1);
				
				if(strArr.length<4){
					continue ;
				}
				
				cateMap.put(strArr[1]+","+strArr[2], strArr[3]);
			}
			return cateMap ;
		} catch (IOException e) {
			e.printStackTrace();
			return null ;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static Map<String ,String> getSourceMap (){
		Map<String ,String > cateMap = new HashMap<String ,String  >();
		
		BufferedReader in = null;

		try {
			// 从当前作业中获取要缓存的文件
//			Path[] paths = DistributedCache.getLocalCacheFiles(job);
			
//			System.out.println("paths===="+paths);
			String line = null;
			String[] strArr = null;
			
			File path = new File ("/Users/sunhongxi/Documents/sshfileupdown/sourcetype.txt") ;
			
			in = new BufferedReader((new InputStreamReader(new FileInputStream(path),"utf-8")));
			while (null != (line = in.readLine())) {
				strArr = line.split("\t", -1);
				
				if(strArr.length<4){
					continue ;
				}
				
				if("3".equals(strArr[0])){
					cateMap.put(strArr[4], strArr[0]+"@"+strArr[1]);
				}
			}
			return cateMap ;
		} catch (IOException e) {
			e.printStackTrace();
			return null ;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
